﻿
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
-----------------------------------------------------------
Ver:V1.1
Created by: shinxu
Date:2023-09-25
Changelog:对微信导入的图片进行处理，为wx_camera文件夹，Other只放置未被分类的其它图片或视频。
------------------------------------------------------------

'''
import os
import shutil
import datetime

########################################################
# 创建指定目录： create_file_path(str)
# 功能 ： 
########################################################
def create_file_path(New_folder):
    if not os.path.exists(New_folder):
        print('OK , I will created  The New path!' + New_folder)
        os.mkdir(New_folder)

########################################
#名称：files_years(文件名，年份列表)
#     返回年份或0
########################################7
def files_years(name,list_addr):
    # 先判断年份string.isdigit()，能判断的，建立分支，否则不处理。
    # 文件可能为2013-12-07_152.jpg;   201312071093.jpg
    #          IMG20120526_002.jpg;
    #          IMG_20230315_151212.jpg;
    #          wx_camera
    if (name[4:8].isdigit()) and (name[4:8] in list_addr):
        #print('命中IMG_20230315_151212.jpg: ' + name)
        picYear = name[4:8]               
        return picYear
    elif (name[3:7].isdigit()) and (name[3:7] in list_addr):
        #print('命中IMG2023年份的：' + name)
        picYear = name[3:7]
        return picYear
    elif (name[0:4].isdigit()) and (name[0:4] in list_addr):
        #print('命中开头为年份的文件：' + name)
        picYear = name[0:4]
        return picYear
    elif ('mmex' in name):
        # 导入视频、文件,不是之前的规则
        return "mmexport"
    elif ('wx_camer' in name):
        # 微信 不同规则
        return "wx_camera"
    else:
        #print("其它文件,不做变更:" + name)
        return '0'
#################################


################################################################
# 文件名：文件处理 MoveFiles(旧文件目录，目标目录，年份例表)
# 说明：  所有文件在年份列表里才能移动，否则不处理。
################################################################
def MoveFiles(old_folder,targatPath,list_addr):
    file_to_targatPath = ""
    for root,dirs, files in os.walk(old_folder, topdown=False):
        for name in files:
            picYear = files_years(name,list_addr)
            file_from_path = os.path.join(root,name)
            if (picYear != '0') and (('jpg' in name) or ('mp4' in name) or ('mmexport' in name) or ('wx_camera' in name) ):
                print(picYear)
                file_to_targatPath = os.path.join(targatPath,picYear)
                create_file_path(file_to_targatPath)           # 判断是否需要在目标中创建年份目录
            elif ('jpg' in name) or ('mp4' in name) :
                # 其它照片文件，视频文件,放入other
                file_to_targatPath = os.path.join(targatPath,"other")
                create_file_path(file_to_targatPath)           # 判断是否需要在目标中创建other目录                
            
            if (len(file_to_targatPath) != 0) and (('jpg' in name) or ('mp4' in name) or ('mmexport' in name) or ('wx_camera' in name) ):
                ## 上方为计算 targatPath 是指定还是other
                if os.path.exists(os.path.join(file_to_targatPath,name)):
                    print("文件重复，删除源文件之...")
                    os.remove(file_from_path)
                else:
                    shutil.move(file_from_path,file_to_targatPath)
                    print(name)

         
##############################################
# Begin...
##############################################

pathfile = "Z:\\2020"
#pathfile = "H:\\Test_NP"
#targatPath = "Z:\\xu\\pic2023zl"
targatPath = "Z:\\photo_auto2023"

pic_yearlist = ['2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022','2023']
 
ThisYear = datetime.datetime.today().year

print("Beginning ....")
if str(ThisYear) not in pic_yearlist:
    print("must add year")
    pic_yearlist.append(str(ThisYear))

MoveFiles(pathfile,targatPath,pic_yearlist)
print("End ....")